package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@Slf4j
@Api(tags = "管理端套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增套餐")
    @CachePut(cacheNames = "setmealCache",key = "setmealDTO.categoryId")
    public Result setmealsave(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐 {}",setmealDTO);
        setmealService.setmealsave(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    private Result<SetmealVO> getByid(@PathVariable Long id){
        log.info("根据id查询套餐 {}" ,id);
        SetmealVO setmealVO=setmealService.getByid(id);
        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result removeByid(@RequestParam List<Long> ids){
        log.info("批量删除套餐 {}" ,ids);
        setmealService.removeByid(ids);
        return Result.success();
    }

    /**
     * 套餐停售启售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐停售启售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmealStatus(@PathVariable Integer status,@RequestParam Long id){
        log.info("修改套餐状态为{} {}",status,id);
        setmealService.upsateSetmealStatus(status,id);
        return Result.success();
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改套餐信息")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息 {}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询")
    public Result<PageResult> setmealPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐分页查询 {}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.setmealPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
}
